# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer:
pkgname=idris2
pkgver=0.5.1
# XXX: For chez-scheme rebuilds, re-bootstrap from -stage0 and
# afterwards self-host by depending on idris2-bootstrap again.
pkgrel=4
pkgdesc="Purely functional programming language with first class types"
url="https://www.idris-lang.org"
arch="x86 x86_64" # limited by chez-scheme
license="BSD-3-Clause"
depends="chez-scheme gmp-dev"
makedepends="idris2-bootstrap bash py3-sphinx py3-sphinx_rtd_theme"
subpackages="$pkgname-doc $pkgname-api"
source="https://github.com/idris-lang/Idris2/archive/v$pkgver/idris2-$pkgver.tar.gz
	0001-fix-Flush-standard-when-prompting-for-package-inform.patch"
builddir="$srcdir/Idris2-$pkgver"

# Perform the idris2 self-hosting step by re-compiling Idris2
# using the previous version of Idris2 or idris2-stage0. This
# allows us to (a) compile the Idris2 API and (b) backport
# patches from upstream.
#
# See also https://lists.alpinelinux.org/~alpine/devel/%3C33KG0XO61I4IL.2Z7RTAZ5J3SY6%408pit.net%3E
provides="idris2-bootstrap"
provider_priority=100 # highest

case "$CARCH" in
x86) options="!check" ;; # XXX: https://github.com/idris-lang/Idris2/issues/1617
esac

# TODO: Install idris libs to /usr/lib/idris-$pkgver instead of /usr/idris-$pkgver
# TODO: Self-hosting step (maybe split into idris2 and idris2-bootstrap?)
# TODO: https://github.com/idris-lang/Idris2/pull/1123

build() {
	make -j1 PREFIX=/usr SCHEME=chez
	make -C docs html
}

check() {
	make test
}

package() {
	# Idris2 Makefile doesn't honor ${DESTDIR}
	# See https://github.com/idris-lang/Idris2/pull/1123
	export IDRIS2_BOOT=./build/exec/idris2
	make -j1 IDRIS2_PREFIX="$pkgdir/usr" \
		PREFIX="$pkgdir/usr" install install-api install-libdocs

	# Move package documentation to more sensible location.
	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	mv "$pkgdir"/usr/idris2-$pkgver/docs/ \
		"$pkgdir"/usr/share/doc/$pkgname/pkgs

	# Install additional documentation files
	install -m644 CHANGELOG.md CONTRIBUTING.md \
		CONTRIBUTORS README.md "$pkgdir"/usr/share/doc/$pkgname/
	cp -r docs/build/html "$pkgdir"/usr/share/doc/$pkgname/

	# We don't want to ship directories in $PATH. The /usr/bin/idris2
	# file installed by default is just a shell script which sets up
	# LD_LIBRARY_PATH and executes idris2_app/idris2.so. We don't
	# need to modify LD_LIBRARY_PATH hence we can skip using the
	# script.
	#
	# See also: The Nix package for idris2 in nix/package.nix.
	mv "$pkgdir"/usr/bin/idris2_app/idris2.so "$pkgdir"/usr/bin/idris2
	rm -r "$pkgdir"/usr/bin/idris2_app

	# Make sure idris2 --install works by default.
	# See https://github.com/idris-lang/Idris2/issues/737
	mkdir -p "$pkgdir"/etc/profile.d/
	cat > "$pkgdir"/etc/profile.d/idris2.sh <<-EOF
		export IDRIS2_PREFIX="$HOME/.idris2"
		export IDRIS2_PACKAGE_PATH="/usr/idris2-$pkgver"
		export IDRIS2_LIBS="/usr/idris2-$pkgver/lib"
		export IDRIS2_DATA="/usr/idris2-$pkgver/support"
	EOF
}

api() {
	depends=""
	pkgdesc="Idris API for developing support tools (e.g. external code generator)"

	amove usr/idris2-$pkgver/idris2-$pkgver
}

sha512sums="
6b4a4092570136a684bdd80765d271bc1ebe163c273ff22d22c808f586183a8e90a326573dd0e4b339e1ab0b88fd31f1cc7bf2820ff7bab2742c4156b816c5b7  idris2-0.5.1.tar.gz
e3a54ee4a57e2089290fd6d9f69b374b04bf1600f6779dd514e513d59486c4bb51514952ed5019abb016abecf4a32e422996184854e4da32e4c7713eb8db2e46  0001-fix-Flush-standard-when-prompting-for-package-inform.patch
"
